export const transformNumberToWords = (number = 0) => {
    if (number < 1000) return number;
    if (number) {
        const _number = number.toString()
        if (_number.length > 8) {
            return (_number / 100000000).toFixed(1) + '亿'
        } else {
            return (_number / 10000).toFixed(1) + '万'
        }
    }
}

export function throttle(func, wait) {
    var previous = 0;
    return function () {
        var now = Date.now();
        var context = this;
        var args = arguments;
        if (now - previous > wait) {
            func.apply(context, args);
            previous = now;
        }
    }
}

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait = 300, immediate) {
    var timeout;
    return function () {
        var context = this;
        var args = arguments;
        if (timeout) clearTimeout(timeout);
        if (immediate) {
            var callNow = !timeout;
            timeout = setTimeout(function () {
                timeout = null;
            }, wait)
            if (callNow) func.apply(context, args)
        } else {
            timeout = setTimeout(function () {
                func.apply(context, args)
            }, wait);
        }
    }
}

export const transformNumberToTime = (number) => {
    if (number) {
        const timeNumber = Number(number);
        const min = timeNumber / 60;
        const sec = timeNumber % 60;
        return `${min >= 10 ? `${Math.floor(min)}` : `0${Math.floor(min)}`}:${sec >= 10 ? `${Math.floor(sec)}` : `0${Math.floor(sec)}`}`
    } else {
        return `00:00`
    }
}
export const dealLyrics = (str = '') => {
    if (typeof str !== 'string') return
    const timeStringReg = new RegExp(/\[(\d+:\d+.\d+)]/g);
    const MSMReg = new RegExp(/\d+/g)
    let lrcArr = str.split(timeStringReg)//使用正则匹配出来的字符切割
    lrcArr.shift()//去掉切出的空字符串
    let timingSet = new Set()
    const lycData = lrcArr.reduce((acc, current, index, arr) => {
        if (index % 2 == 0 && current && current !== "\n" && arr[index + 1] !== "\n" && !timingSet.has(current)) {//偶数为时间
            timingSet.add(current)
            const timeSeconds = current.match(MSMReg).map(item => Number(item))
            const timeSecond = timeSeconds[0] * 60 + timeSeconds[1] + timeSeconds[2] / 1000
            return [...acc, {timing: timeSecond, text: arr[index + 1]}]
        } else {
            return acc
        }
    }, [])
    return lycData;
}
